<!-- prettier-ignore -->
<template>
  <div id="userRegisterView">
    <!-- 双栏布局注册页 -->
    <div class="register-container">
      <!-- 左侧欢迎区域 -->
      <div class="welcome-panel">
        <h1>Welcome To AIGC-OJ</h1>
        <p>Already have an account?</p>
        <a-button class="login-btn" @click="gologin">Login</a-button>
      </div>
      
      <!-- 右侧注册表单 -->
      <div class="register-panel">
        <h2 class="register-title">Register</h2>
        
        <a-form :model="form" class="register-form" @submit="handleSubmit">
          <a-form-item field="userAccount">
            <a-input
              v-model="form.userAccount"
              placeholder="Username"
              allow-clear
            >
              <template #prefix>
                <icon-user />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item field="userPassword">
            <a-input-password
              v-model="form.userPassword"
              placeholder="Password"
              allow-clear
            >
              <template #prefix>
                <icon-lock />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item field="checkPassword">
            <a-input-password
              v-model="form.checkPassword"
              placeholder="Confirm Password"
              allow-clear
            >
              <template #prefix>
                <icon-lock />
              </template>
            </a-input-password>
          </a-form-item>

          <a-button
            type="primary"
            html-type="submit"
            :loading="loading"
            class="submit-button"
          >
            Register
          </a-button>

          <div class="terms-privacy">
            <p>By registering, you agree to our <a-link>Terms</a-link> and <a-link>Privacy Policy</a-link></p>
          </div>
          
          <div class="social-register">
            <p>or register with social platforms</p>
            <div class="social-icons">
              <a-button shape="circle" class="social-icon">
                <icon-google />
              </a-button>
              <a-button shape="circle" class="social-icon">
                <icon-facebook />
              </a-button>
              <a-button shape="circle" class="social-icon">
                <icon-github />
              </a-button>
              <a-button shape="circle" class="social-icon">
                <icon-link />
              </a-button>
            </div>
          </div>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { UserControllerService, UserRegisterRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import {
  IconUser,
  IconLock,
  IconGoogle,
  IconFacebook,
  IconGithub,
  IconLink,
} from "@arco-design/web-vue/es/icon";

const router = useRouter();
const loading = ref(false);

const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
} as UserRegisterRequest);

/**
 * 提交表单
 */
const handleSubmit = async () => {
  // 表单校验
  if (!form.userAccount) {
    message.error("请输入账号");
    return;
  }
  if (!form.userPassword) {
    message.error("请输入密码");
    return;
  }
  if (form.userPassword !== form.checkPassword) {
    message.error("两次输入密码不一致");
    return;
  }

  loading.value = true;
  try {
    const res = await UserControllerService.userRegisterUsingPost(form);
    if (res.code === 0) {
      message.success("注册成功");
      // 跳转到登录页
      router.push({
        path: "/user/login",
      });
    } else {
      message.error("注册失败，" + res.message);
    }
  } catch (e: any) {
    message.error("注册失败，" + e.message);
  } finally {
    loading.value = false;
  }
};

// 跳转到登录页
const gologin = () => {
  router.push({
    path: "/user/login",
  });
};
</script>

<style scoped>
#userRegisterView {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(120deg, #e0f2fe, #f5f7fa);
  padding: 20px;
}

.register-container {
  display: flex;
  width: 900px;
  height: 640px;
  background-color: white;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

/* 左侧欢迎区域 */
.welcome-panel {
  width: 40%;
  background-color: #7aaaff;
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 50px;
  text-align: center;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
}

.welcome-panel h1 {
  font-size: 2.8rem;
  font-weight: 600;
  margin-bottom: 20px;
}

.welcome-panel p {
  margin-bottom: 40px;
  font-size: 1.1rem;
}

.login-btn {
  background-color: transparent;
  border: 2px solid white;
  color: white;
  padding: 5px 20px;
  border-radius: 20px;
  font-weight: 500;
  width: 150px;
}

.login-btn:hover {
  background-color: white;
  color: #7aaaff;
}

/* 右侧注册表单 */
.register-panel {
  width: 60%;
  padding: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.register-title {
  font-size: 2.4rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 40px;
  text-align: center;
}

.register-form {
  width: 100%;
}

:deep(.arco-input-wrapper) {
  border-radius: 5px;
  padding: 10px;
  margin-bottom: 5px;
  background-color: #f5f7fa;
  border: none;
}

:deep(.arco-input) {
  color: #333;
  background-color: #f5f7fa;
}

:deep(.arco-input-prefix) {
  margin-right: 10px;
  color: #999;
}

.submit-button {
  width: 100%;
  height: 45px;
  border-radius: 5px;
  font-size: 1rem;
  font-weight: 500;
  margin-top: 10px;
  background-color: #4080ff;
}

.terms-privacy {
  margin-top: 15px;
  text-align: center;
  font-size: 0.8rem;
  color: #666;
}

.social-register {
  margin-top: 25px;
  text-align: center;
}

.social-register p {
  color: #999;
  margin-bottom: 15px;
  font-size: 0.9rem;
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.social-icon {
  border: 1px solid #ddd;
  background-color: white;
  color: #666;
  font-size: 1.2rem;
}

.social-icon:hover {
  background-color: #f5f7fa;
  color: #4080ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
    height: auto;
    width: 100%;
    max-width: 500px;
  }
  .welcome-panel,.register-panel {
    width: 100%;
    border-radius: 0;
  }
  .welcome-panel {
    padding: 30px;
    border-top-right-radius: 20px;
    border-bottom-left-radius: 0;
  }
}
</style>
